package com.autocar.reader.view;

import java.io.File;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.autocar.reader.R;
import com.autocar.reader.bean.NewsBean;
import com.autocar.reader.net.AsynThreadManager;
import com.autocar.reader.util.ConstantData;
import com.autocar.reader.util.FileHandler;
import com.autocar.reader.util.ImageUtils;
import com.autocar.reader.view.MyBannerViewGroup.IMyBannerGroupView;
import com.org.any.autocar.fragment.IopenArticle;

public class XListViewBanner extends LinearLayout implements IMyBannerGroupView {

	private MyBannerViewGroup groupview;
	private MyPointView pointview;
	private LinearLayout bglayout;
	private TextView infotv;
	private List<NewsBean> listBanner;
	// private FileHandler fileHandler;
	private IopenArticle iopen;
	private View banner;

	public XListViewBanner(Context context) {
		super(context);
		initview(context);
	}

	public XListViewBanner(Context context, AttributeSet attrs) {
		super(context, attrs);
		initview(context);
	}

	private void initview(Context context) {
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		banner = LayoutInflater.from(context).inflate(
				R.layout.xlistview_banner, null);
		addView(banner, lp);
		groupview = (MyBannerViewGroup) banner
				.findViewById(R.id.xlistview_banner_mybannergroup);
		pointview = (MyPointView) banner
				.findViewById(R.id.xlistview_banner_pointview);
		bglayout = (LinearLayout) banner
				.findViewById(R.id.xlistview_banner_linearl_bg);
		infotv = (TextView) banner.findViewById(R.id.xlistview_banner_tv_info);
		bglayout.getBackground().setAlpha(150);

		groupview.setIMyBannerGroupView(this);

		// fileHandler = new FileHandler();

		banner.setVisibility(GONE);

	}

	public void setListNews(List<NewsBean> listBanner, IopenArticle iopen) {
		this.iopen = iopen;
		this.listBanner = listBanner;
		int size = listBanner.size();
		changePage(0);
		groupview.setInfo(0, size);
		pointview.setTotalCircle(size);
		if (size > 1) {
			loadPage(0, groupview.getImageView(0));
			loadPage(1, groupview.getImageView(1));
			// loadPage(0);
			// loadPage(1);
			// ImageUtils.fb.display(holder.picIv, bean.getS_pic());
			// ImageUtils.fb.display(holder.picIv, bean.getS_pic());
		} else {
			// loadPage(0);
			loadPage(0, groupview.getImageView(0));
		}
	}

	private Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.obj != null) {
				String filePath = msg.obj.toString();
				int page = msg.arg1;
				if (filePath != null) {
					groupview.setImage(getBitmap(filePath), page);
				}
			}
		}
	};

	private Bitmap getBitmap(String filePath) {
		return BitmapFactory.decodeFile(filePath);
	}

	private NewsBean getNewsBean(int position) {
		if (position < 0 || position > listBanner.size() - 1) {
			return null;
		}
		return listBanner.get(position);
	}

	@Override
	public void loadPage(final int targetPage, ImageView view) {
		final NewsBean bean = getNewsBean(targetPage);
		if (bean == null) {
			return;
		}
//		ImageUtils.fb.display(view, bean.getS_pic(), 700, 700);
		final String filePath = ConstantData.getImagePath(bean.getS_pic());
		if (new File(filePath).exists()) {
			sendMessage(targetPage, filePath);
			return;
		}
		AsynThreadManager.execuSingleTask(new Runnable() {
			@Override
			public void run() {
				FileHandler fileHandler = new FileHandler();
				String tfilePath = fileHandler.saveImg(bean.getS_pic(),
						filePath);
				sendMessage(targetPage, tfilePath);
			}
		});
	}

	private void sendMessage(int page, String filePath) {
		Message msg = myHandler.obtainMessage();
		msg.what = 0;
		msg.arg1 = page;
		msg.obj = filePath;
		myHandler.sendMessage(msg);
	}

	@Override
	public void changePage(int targetPage) {
		final NewsBean bean = getNewsBean(targetPage);
		if (bean == null) {
			return;
		}
		pointview.setSelectId(targetPage);
		String title = bean.getTitle();
		infotv.setText(title);
	}

	public void setBannerView(boolean visible) {
		if (visible) {
			banner.setVisibility(VISIBLE);
		} else {
			banner.setVisibility(GONE);
		}
	}

	@Override
	public void onClick(int targetPage) {
		if (iopen != null) {
			final NewsBean bean = getNewsBean(targetPage);
			if (bean == null) {
				return;
			}
			iopen.opentArticle(bean);
		}
	}

}
